﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SalesPosting();
        }
    }

    private void SalesPosting()
    {
        string sql = "select distinct WorkStationID,LocalSalesMasterID from Pos_SalesMaster where StatusID=0 ;";
        sql += "select distinct WorkStationID,ServerTransactionMasterID from Pos_SalesMaster where StatusID=-1 ;";
        DataSet ds = MSSQL.SQLExec(sql);

        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            try
            {
                if (CommonManager.ProcessSalesServer(dr["WorkStationID"].ToString(), dr["LocalSalesMasterID"].ToString()))
                {
                    sql = "Update Pos_SalesMaster set StatusID=1 where WorkStationID=" + dr["WorkStationID"].ToString() + " and LocalSalesMasterID=" + dr["LocalSalesMasterID"].ToString();

                    MSSQL.SQLExec(sql);
                    //Sendmail.sendEmail("anamuliut@gmail.com", "GP(" + ConfigurationManager.AppSettings["WorkStationID"] + @") Database Update Successfully . ", sql);
                }
                else
                {
                    Sendmail.sendEmail("anamuliut@gmail.com", "Sales Data is not posting . ", dr["WorkStationID"].ToString() + " Br Sales ID-" + dr["LocalSalesMasterID"].ToString() + " gives Return false<hr/>" + sql);
                }
            }
            catch (Exception ex)
            {
                Sendmail.sendEmail("anamuliut@gmail.com", "GP(" + ConfigurationManager.AppSettings["WorkStationID"] + @") Database Update problem .", ex.Message + "<hr/>" + sql);
            }
        }

        foreach (DataRow dr in ds.Tables[1].Rows)
        {
            try
            {
                if (CommonManager.DeleteWholeTransactionMaster(int.Parse(dr["ServerTransactionMasterID"].ToString())) == "Delete Successfully")
                {
                    sql = "Update Pos_SalesMaster set Status=3 where WorkStationID=" + dr["WorkStationID"].ToString() + " and ServerTransactionMasterID=" + dr["ServerTransactionMasterID"].ToString();

                    MSSQL.SQLExec(sql);
                    //Sendmail.sendEmail("anamuliut@gmail.com", "GP(" + ConfigurationManager.AppSettings["WorkStationID"] + @") Database Update Successfully . ", sql);
                }
                else
                {
                    Sendmail.sendEmail("anamuliut@gmail.com", "Sales Data is not posting . ", dr["WorkStationID"].ToString() + " Br Sales ID-" + dr["LocalSalesMasterID"].ToString() + "<hr/>" + sql);
                }
            }
            catch (Exception ex)
            {
                Sendmail.sendEmail("anamuliut@gmail.com", "GP(" + ConfigurationManager.AppSettings["WorkStationID"] + @") Database Update problem .", ex.Message + "</hr>" + sql);
            }
        }
    }
}